<?php

namespace app\admin\controller;

use app\common\controller\Admin;
use think\Request;

class Glzx extends Admin
{
    public function index()
    {
        $where = [];
        if(session('login')['master'] == 1){
        	$where[] = ['id','=',session('login')['glzx_id']];
        }
        if($this->request->isPost())
        {
            $post = post();
            if(isset($post['name'])) $where[] = ['name','like','%' . $post['name'] . '%'];
            if(isset($post['status'])) $where[] = ['status','=',$post['status']];
            $list = ysh_select($this->request->controller(),['where'=>$where]);
        }else{
            $list = ysh_select($this->request->controller(),['where'=>$where]);
        }
        //dump($list);
        $this->assign('list',$list['list']['data']);
        //page
        $this->assign('page',$list['page']);
        //总个数
        $this->assign('count',$list['list']['total']);
        //显示条数
        $this->assign('limit',$list['list']['per_page']);
        //当前页数
        $this->assign('curl',$list['list']['current_page']);
        //页码数
        $this->assign('groups',$list['list']['last_page']);
        return $this->fetch();
    }
    public function indexs()
    {
        $id = get()['id'];
        $this->assign('id',$id);
        $where = [];
        $where['where'][] = ['fwgl_id','=',$id];
        if($this->request->isPost())
        {
            $post = post();
            if(isset($post['batterybaid'])) $where['where'][] = ['batterybaid','like','%' . $post['batterybaid'] . '%'];
            $list = ysh_select('batteryba',$where);
        }else{
            $list = ysh_select('batteryba',$where);
        }
        $this->assign('list',$list['list']['data']);
        //page
        $this->assign('page',$list['page']);
        //总个数
        $this->assign('count',$list['list']['total']);
        //显示条数
        $this->assign('limit',$list['list']['per_page']);
        //当前页数
        $this->assign('curl',$list['list']['current_page']);
        //页码数
        $this->assign('groups',$list['list']['last_page']);
        return $this->fetch();
    }
	public function add()
	{
		if($this->request->isPost())
		{
			$post = post();
			// ysh_save('glzx',$post);
			//缴纳180万资格费，获得200%的管理权益积分
			$num = 180*10000*2;
			model('integral')->save(['source'=>17,'user_id'=>$post['user_id'],'status'=>1,'num'=>$num]);
			$integralid = model('integral')->id;
			//管理中心的权益积分会被分为：（39.5%无息贷款额度）（15%产品复销）（39.5%股票）（6%税收）
			$all = [];
			$all[] = ['integral_id'=>$integralid,'user_id'=>$post['user_id'],'status'=>3,'num'=>$num*0.395,'class'=>1];
			$all[] = ['integral_id'=>$integralid,'user_id'=>$post['user_id'],'status'=>8,'num'=>$num*0.15,'class'=>1];
			$all[] = ['integral_id'=>$integralid,'user_id'=>$post['user_id'],'status'=>7,'num'=>$num*0.395,'class'=>1];
			$all[] = ['integral_id'=>$integralid,'user_id'=>$post['user_id'],'status'=>6,'num'=>$num*0.06,'class'=>1];
			model('integraldetailed')->saveAll($all);
			model('glzx')->save($post);
			return $this->redirect('index');
		}
		return $this->fetch();
	}
	public function upssss()
    {
        if($this->request->isPost())
        {
            $post = post();
            if(!isset($post['username'])) return false;
            $list = ysh_select('user',['where'=>
                [['username','=',$post['username']]]
            ],2);
            	$list['code'] = 200;
            return $list;
        }
    }
    public function upload()
    {
        $id = get()['id'];
        $this->assign('id',$id);
        if($this->request->isPost())
        {
            $post = post();
            $post['id'] = $id;
            ysh_upload('glzx',$post);
            return $this->redirect('index');
        }
        $list = ysh_select('glzx',['where'=>[['id','=',$id]],'with'=>'business.user'],2);
        $idall = [];
        $user = [];
        // pr($list);
        foreach ($list['list']['business'] as $v){
        // pr($v);
        	if(!in_array($v['user']['id'],$idall)){
        		$idall[] = $v['user']['id'];
        		$user[] = $v['user']['username'];
        	}
        }
        $this->assign('userall',implode(',',$user));
        $this->assign('list',$list['list']);
        return $this->fetch();
    }
    public function ss()
    {
        if($this->request->isPost())
        {
            $post = post();
            if(!isset($post['batterybaid'])) return false;
            $list = ysh_select('batteryba',['where'=>
                [['batterybaid','=',$post['batterybaid']]]
            ],2);
            return $list;
        }
    }
    public function fp()
    {
        if($this->request->isPost())
        {
            $post = post();
            ysh_upload('batteryba',['id'=>$post['sid'],'fwgl_id'=>$post['id']]);
            return true;
        }
    }
    public function upss()
    {
        if($this->request->isPost())
        {
            $post = post();
            if(!isset($post['username'])) return false;
            $list = ysh_select('user',['where'=>
                [['username','=',$post['username']]]
            ],2);
            return $list;
        }
    }
    public function upfp()
    {
        if($this->request->isPost())
        {
            $post = post();
            $list = ysh_select('business',['where'=>[['user_id','=',$post['sid']]]],2);
            // pr($list);
            $model = ysh_upload('business',['id'=>$list['list']['id'],'fwgl_id'=>$post['id']]);
            return $model;
        }
    }
    public function wallet(){
        $get = input('get.');
        //条数
        $limit = $get['limit']?? 10;
        //当前页数
        $curl = $get['page']?? 1;
        $name = $this->request->controller() . $this->request->action();
        $where = [];
        $pid = ['orderids'=>['battery'],'pids','user'=>['business','fwgl','glzx']];
        if(session('login')['master'] == 1){
        	$where[] = ['user_id','=',session('login')['id']];
        }
        $where[] = ['class','in','5,10'];
        $order = 'id desc';
        if($this->request->isPost())
        {
            $post = input('post.');
            //订单编号
            $where['orderid'] = $post['orderid']??'';
            //开始结束时间
            $where['time'] = $post['time']??'';
            //状态
            $where['class'] = $post['class']??'';
            $wheres =[];
            if($where['time']){
                list($first,$last) = explode('至',$where['time']);
                $wheres[] = ['create_time','>',trim($first)];
                $wheres[] = ['overtime','<',trim($last)];
            }
            foreach ($where as $k=>$v){
                if(strlen($v)>0){
                    if($k !== 'time') $wheres[] = [$k,'=',$v];
                }
            }
            if($wheres){
                session($name,$wheres);
                $list = model('wallet')->order($order)->with($pid)->where($wheres)->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model('wallet')->where($wheres)->select()->toArray());
            }else{
                Session::delete($name);
                $list = model('wallet')->order($order)->with($pid)->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model('wallet')->select()->toArray());
            }
        }else{
            if(session($name)){
                $list = model('wallet')->order($order)->where(session($name))->with($pid)->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model('wallet')->where(session($name))->select()->toArray());
            }else{
                $list = model('wallet')->order($order)->where($where)->with($pid)->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model('wallet')->where($where)->select()->toArray());
            }
        }
        foreach($list as $k=>$v){
        	if(isset($v['orderids']['battery']['batteryba_id'])){
        		if($v['orderids']['battery']['batteryba_id'] > 0){
        			$bus = model('batteryba')->where('id','=',$v['orderids']['battery']['batteryba_id'])->with('business')->find()->toArray();
        			$list[$k]['busname'] = $bus['business']['name'];
        		}
        		
        	}
        }
        $this->assign('list',$list);
        $this->assign('page',$page);
        //总个数
        $this->assign('count',$count);
        //显示条数
        $this->assign('limit',$limit);
        //当前页数
        $this->assign('curl',$curl);
        //页码数
        $groups = ceil($count/$limit);
        $this->assign('groups',$groups);
        //资产总额
        //进项
        $jmoney = model('wallet')->field('money')->where([['class','=',5],['user_id','=',session('login')['id']]])->select()->toArray();
        $money = 0;
        foreach ($jmoney as $v){
            $money = $v['money']*1+$money;
        }
        //出项
        $jmoney = model('wallet')->field('money')->where([['class','=',10],['user_id','=',session('login')['id']]])->select()->toArray();
        $moneys = 0;
        foreach ($jmoney as $v){
            $moneys = $v['money']*1+$moneys;
        }
        //平台总收益
        $money = $money-$moneys;
        $this->assign('ptsy',$money);
        return $this->fetch();
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
